import java.awt.Point;
import java.util.List;
import java.util.Map;

import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class XSafeSpot extends Script {
	RSTile SAFE_SPOT;
	int MONSTER;
	public int ITPID1 = 0000;
	public int[] ITPIDs;
	public int ITPID2 = 0000;
	public int ITPID3 = 0000;
	int eatAT = 30;
	int count;
	int newcount;
	public int[] FoodID = new int[] { 379, 385, 7946, 333, 2297, 2299 };
	public boolean pickoi = false;

	private boolean ATTACKNPC(final RSNPC npc, final String action) { // edited
		// ruskis
		// a lil
		final RSTile tile = npc.getLocation();
		tile.randomizeTile(1, 1);
		try {
			final int hoverRand = random(8, 13);
			for (int i = 0; i < hoverRand; i++) {
				final Point screenLoc = npc.getScreenLocation();
				if (!pointOnScreen(screenLoc)) {
					setCameraRotation(getCameraAngle() + random(-35, 150));
					return true;
				}

				moveMouse(screenLoc, 15, 15);

				final List<String> menuItems = getMenuItems();
				if (menuItems.isEmpty() || menuItems.size() <= 1) {
					continue;
				}
				if (menuItems.get(0).toLowerCase().contains(
						npc.getName().toLowerCase())
						&& getMyPlayer().getInteracting() == null) {
					clickMouse(true);
					return true;
				} else {
					for (int a = 1; a < menuItems.size(); a++) {
						if (menuItems.get(a).toLowerCase().contains(
								npc.getName().toLowerCase())
								&& getMyPlayer().getInteracting() == null) {
							clickMouse(false);
							return atMenu(action);
						}
					}
				}
			}

		} catch (final Exception e) {
			log.warning("ATTACKNPC(RSNPC, String) error: " + e);
			return false;
		}
		return false;
	}

	void eat(final int id) {
		atInventoryItem(id, "Eat");
		wait(random(1500, 2500));
		return;
	}

	private void eat_check() {
		final RSItem food = getInventoryItemByID(FoodID);
		if (getInventoryCount(FoodID) >= 1) {
			eat(food.getID());
		} else {
			log("Out of food... Way to safe spot");
			logout();
			stopAllScripts();
		}
		return;
	}

	void EATING() {
		final int php = skills.getCurrentSkillLevel(3);
		if (php <= eatAT) {
			eat_check();
		} else {
			wait(random(20, 30));
		}
	}

	@Override
	public String getAuthor() {
		return "Killa";
	}

	@Override
	public String getName() {
		return "XSafeSpot";
	}

	@Override
	public String getScriptCategory() {
		return "Combat";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<head>";
		html += "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>";
		html += "</head>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"6\" color=\"red\">" + getName() + " v"
				+ getVersion() + " c" + "</font></b><br>";
		html += "<font size=\"3\" color=\"red\">START AT SAFE SPOT TILE!!!<br></font>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">";
		html += "<tr><td><b>Options:</b></td>";
		html += "<tr><td><b>Pick Items:</b></td><td colspan=\"3\"><center><b>Item ID 1:</b>&nbsp<input type=\"text\" name=\"ItemID1\" size=\"4\">&nbsp&nbsp<b>Item ID 2:</b>&nbsp<input type=\"text\" name=\"ItemID2\" size=\"4\">&nbsp&nbsp&nbsp<b>Arrows or Bolt IDs:</b>&nbsp<input type=\"text\" name=\"ItemID3\" size=\"4\"></center></td></tr>";
		html += "<tr><td><b>Monster IDs:</b>&nbsp<input type=\"text\" name=\"Mon\" size=\"4\"></center></td></tr>";
		html += "<tr><td colspan=2><b><font size=\"3\">&nbsp&nbsp Thanks for using this script, enjoy it!</font></b></td><td  colspan=\"2\" align=right>"
				+ getVersion() + " &nbsp&nbsp</td></tr></table>";
		html += "</center></body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public int loop() {
		setCameraAltitude(true);
		if (getMyPlayer().isMoving()) {
			return random(0, 150);
		}
		if (!getMyPlayer().getLocation().equals(SAFE_SPOT)
				&& getGroundItemByID(5, ITPIDs) == null) {
			walkTileMM(SAFE_SPOT);
		}
		if (getMyPlayer().isInCombat()) {
			walkTileMM(SAFE_SPOT);
		}
		if (getMyPlayer().getLocation().equals(SAFE_SPOT)
				&& getMyPlayer().getInteracting() == null
				&& getNearestFreeNPCByID(MONSTER) != null) {
			ATTACKNPC(getNearestFreeNPCByID(MONSTER), "Attack");
		}
		if (getMyPlayer().getLocation().equals(SAFE_SPOT)) {
			EATING();
		}
		if (pickoi && getMyPlayer().getInteracting() == null
				&& !getMyPlayer().isInCombat()) {
			pickOI();
		}
		return random(50, 100);
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		SAFE_SPOT = getMyPlayer().getLocation();
		try {
			log(args.get("Mon"));
			MONSTER = Integer.parseInt(args.get("Mon"));
		} catch (final Exception e) {
			log("Invalid Monster");
			return false;
		}
		if (!args.get("ItemID1").equals("")) {
			ITPID1 = Integer.parseInt(args.get("ItemID1"));
			pickoi = true;
		}
		if (!args.get("ItemID2").equals("")) {
			ITPID2 = Integer.parseInt(args.get("ItemID2"));
			pickoi = true;
		}
		if (!args.get("ItemID3").equals("")) {
			ITPID3 = Integer.parseInt(args.get("ItemID3"));
			pickoi = true;
		}
		ITPIDs = new int[] { ITPID1, ITPID2, ITPID3 };
		return true;
	}

	public void pickOI() {
		getInventoryItemByID(ITPIDs);
		final int OldOitemCount = getInventoryCount(ITPIDs);
		final RSItemTile TItem = getGroundItemByID(5, ITPIDs);
		if (TItem != null) {
			atTile(TItem, "Take");
			while (getMyPlayer().isMoving()) {
				wait(15);
			}
			if (getInventoryCount(ITPIDs) > OldOitemCount) {
				count = count + 1;
				log("Picked up item.");
				newcount = count;
				return;
			} else {
				log("No item found.");
				return;
			}
		}
		return;
	}
}